package com.javalong.affairs.module.renwujinzhang.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.javalong.affairs.R;
import com.javalong.affairs.base.activity.BaseActivity;
import com.javalong.affairs.base.common.ViewHolder;

/**
 * Created by 杨观回 on 2016/1/17 0017.
 */
public abstract class ListFragment extends Fragment {
    protected static final int HEADER_VIEW = -100;
    protected static final int FOOTER_VIEW = -200;
    protected static final int NORMAL_VIEW = 0;
    private RecyclerView recyclerView;
    protected LayoutInflater mInflater;
    protected BaseActivity baseActivity;
    public RecyclerView getRecyclerView() {
        return recyclerView;
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        recyclerView = (RecyclerView) inflater.inflate(R.layout.fragment_list, container, false);
        initData();
        afterInit();
        return recyclerView;
    }

    protected void initData() {
        baseActivity = (BaseActivity) getActivity();
        mInflater = LayoutInflater.from(getActivity());
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.setAdapter(new RecycleViewHolder());
    }


    class RecycleViewHolder extends RecyclerView.Adapter<ViewHolder> {

        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return ListFragment.this.onCreateViewHolder(parent, viewType);
        }

        @Override
        public void onBindViewHolder(ViewHolder holder, final int position) {
            ListFragment.this.onBindViewHolder(holder, position);
        }

        @Override
        public int getItemCount() {
            return ListFragment.this.getItemCount();
        }

        @Override
        public int getItemViewType(int position) {
            return ListFragment.this.getItemViewType(position);
        }
    }

    public abstract void afterInit();

    ;

    public abstract ViewHolder onCreateViewHolder(ViewGroup parent, int viewType);

    public abstract void onBindViewHolder(ViewHolder holder, final int position);

    public abstract int getItemViewType(int position);

    public abstract int getItemCount();
}
